package springboot.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;


import springboot.common.Constants;
import springboot.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import springboot.entity.LjhsAddress;
import springboot.entity.LjhsCollector;

import springboot.mapper.LjhsAddressMapper;
import springboot.mapper.LjhsCollectorMapper;
import springboot.service.ILjhsOrderService;
import springboot.entity.LjhsOrder;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hys
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/ljhsorder")
public class LjhsOrderController {
    @Resource
    private ILjhsOrderService ljhsOrderService;
    @Resource
    private LjhsAddressMapper ljhsAddress;
    @Resource
    private LjhsCollectorMapper ljhsCollectorMapper;

    @PostMapping

    public Result save(@RequestBody LjhsOrder ljhsOrder){
        if(ljhsOrder.getCollector_id()==null){
            ljhsOrder.setState("未接单");
        }else{
            ljhsOrder.setState("已接单");
        }
        if(ljhsOrder.getAddress_id()!=null){
            Integer id =ljhsOrder.getAddress_id();
            LjhsAddress ljhsAddress = this.ljhsAddress.selectById(id);
            String city = ljhsAddress.getCity();
            String province = ljhsAddress.getProvince();
            String district = ljhsAddress.getDistrict();
            ljhsOrder.setAddress(province+city+district);
        }
        if(ljhsOrder.getCollector_id()!=null){
            Integer id = ljhsOrder.getCollector_id();
            BigDecimal totalprice = ljhsOrder.getTotalprice();
            LjhsCollector ljhsCollector = ljhsCollectorMapper.selectById(id);
            BigDecimal commission = ljhsCollector.getCommission();
            BigDecimal bigDecimal = totalprice.add(commission);
            ljhsOrder.setTotalprice(bigDecimal);

        }
        return Result.success(ljhsOrderService.saveOrUpdate(ljhsOrder));
        }

        @DeleteMapping("/{id}")

        public Result delete(@PathVariable Long id){

        return Result.success(ljhsOrderService.removeById(id));
        }

        @PostMapping("/batch/del")
        public Result deleteBatch(@RequestBody List<Long> ids){

        return Result.success(ljhsOrderService.removeByIds(ids));
        }
        @GetMapping

        public Result index(){

        return Result.success(ljhsOrderService.list());
        }
    @GetMapping("/collector/{collectorId}")

    public Result findOn(@PathVariable Integer collectorId) {

        if (collectorId == null) {
            Result.error(Constants.CODE_400, "参数错误");
        } else {
            Result.success(ljhsOrderService.list(new QueryWrapper<LjhsOrder>().eq("collector_id", collectorId)));
        }
        return  Result.success(ljhsOrderService.list(new QueryWrapper<LjhsOrder>().eq("collector_id", collectorId)));
    }
        @GetMapping("/{userId}")

        public Result findOne(@PathVariable Integer userId) {

            if (userId == null) {
                Result.error(Constants.CODE_400, "参数错误");
            } else {
                Result.success(ljhsOrderService.list(new QueryWrapper<LjhsOrder>().eq("user_id", userId)));
            }
            return  Result.success(ljhsOrderService.list(new QueryWrapper<LjhsOrder>().eq("user_id", userId)));
        }

        @GetMapping("/page")

        public Result findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String state
                               ) {
            IPage<LjhsOrder> page = new Page<>(pageNum, pageSize);
            QueryWrapper<LjhsOrder> queryWrapper = new QueryWrapper<>();
            if (!"".equals(state)) {
                queryWrapper.like("state",state);
            }
            queryWrapper.orderByDesc("orderId");
            return Result.success(ljhsOrderService.page(page,queryWrapper));
        }





}

